package com.maila.calltaxi_driver.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.maila.calltaxi_driver.R;
import com.maila.calltaxi_driver.utils.EventData;
import com.maila.calltaxi_driver.utils.NullCheckUtil;
import com.maila.calltaxi_driver.utils.StartActivityUtils;
import com.maila.calltaxi_driver.view.DaulPickerPop;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/3.
 */

public class RegisterThreActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.title_login)
    TextView titleLogin;
    @BindView(R.id.register_header)
    AutoRelativeLayout registerHeader;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_passwd_confirm)
    EditText etPasswdConfirm;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_select_time)
    TextView tvSelectTime;
    @BindView(R.id.et_carcode)
    EditText etCarcode;
    @BindView(R.id.et_unincode)
    EditText etUnincode;
    @BindView(R.id.btn_confirm_register)
    TextView btnConfirmRegister;
    DaulPickerPop timepicker;
    String worktime = "";
    String pwd = "";
    String pwd2 = "";
    String name = "";
    String carcode = "";
    String uniqecode = "";
    Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registerthred_activity);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        context = this;
        timepicker = new DaulPickerPop(this);

        setListener();

    }

    private void setListener() {
        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    pwd = s.toString();
                } else {
                    pwd = "";
                }
            }
        });
        etPasswdConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 5) {
                    if (s.toString().equals(pwd)) {
                        pwd2 = pwd;
                    } else {
                        Toast.makeText(context, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                        pwd = "";
                    }
                }
            }
        });
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 1) {
                    name = s.toString();
                } else {
                    name = "";
                }
            }
        });
        etCarcode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 6) {
                    carcode = s.toString();
                } else {
                    carcode = "";
                }
            }
        });
        etUnincode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 3) {
                    uniqecode = s.toString();
                } else {
                    uniqecode = "";
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRegisterEvent(EventData data) {
        switch (data.getType()) {
            case EventData.EVENT_TYPE_SELECTE_TIME:
                worktime=data.getObject().toString();
                tvSelectTime.setText(data.getObject().toString());
                break;
        }
    }

    @OnClick({R.id.white_return_iv, R.id.tv_select_time, R.id.btn_confirm_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.tv_select_time:
                timepicker.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
                break;
            case R.id.btn_confirm_register:
                if (NullCheckUtil.checkNullPoint(pwd)) {
                    if (NullCheckUtil.checkNullPoint(name)) {
                        if (NullCheckUtil.checkNullPoint(worktime)) {
                            if (NullCheckUtil.checkNullPoint(carcode)) {
                                if (NullCheckUtil.checkNullPoint(uniqecode)) {
                                    Toast.makeText(context, "注册成功！", Toast.LENGTH_SHORT).show();
                                    StartActivityUtils.start(context, MainActivity.class);
                                    finish();
                                } else {
                                    Toast.makeText(context, "请选择正确的识别码", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(context, "请选择正确的车牌号", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(context, "请选择正确的工作时间", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "请输入正确的姓名", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(context, "请输入两次正确的密码", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
